package cn.flant.validate;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.flant.util.ArrayUtil;

public class Validate {
	private static final Pattern NUMBER_PATTERN = Pattern.compile("^\\d+(\\.\\d+)?");
	private static final Pattern IP32_PATTERN = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
	private static final Pattern EMAIL_PATTERN = Pattern
			.compile("^([0-9a-zA-Z][\\w+\\-~\\.]*[\\w\\-~])@([0-9a-zA-Z][0-9a-zA-Z\\-]*\\.)+[a-zA-Z]{2,}$");
	private static final Pattern MOBILE_PATTERN = Pattern.compile("^(1\\d{2})\\d{8}$");
	private static final Set<String> MOBILE_10086 = ArrayUtil.asSet(new String[] { "134", "135", "136", "137", "138", "139", "147", "150", "151",
			"152", "157", "158", "159", "182", "183", "184", "187", "188" });
	private static final Set<String> MOBILE_10010 = ArrayUtil.asSet(new String[] { "130", "131", "132", "145", "155", "156", "185", "186" });
	private static final Set<String> MOBILE_10000 = ArrayUtil.asSet(new String[] { "133", "153", "180", "181", "189" });
	private static final Set<String> MOBILE_PREFIX = new HashSet<String>();
	static {
		MOBILE_PREFIX.addAll(MOBILE_10086);
		MOBILE_PREFIX.addAll(MOBILE_10010);
		MOBILE_PREFIX.addAll(MOBILE_10000);
	}

	public static boolean isBlank(String str) {
		return str == null || str.trim().length() == 0;
	}

	public static boolean hasText(String str) {
		return str != null && str.trim().length() > 0;
	}

	public static boolean isNumber(String str) {
		return NUMBER_PATTERN.matcher(str).matches();
	}

	public static boolean isIp(String str) {
		return IP32_PATTERN.matcher(str).matches();
	}

	public static boolean isEmail(String str) {
		return str.length() <= 64 && EMAIL_PATTERN.matcher(str).matches();
	}

	public static boolean isMobile(String str) {
		Matcher m = MOBILE_PATTERN.matcher(str);
		if (m.find()) {
			String prefix = m.group(1);
			if (MOBILE_PREFIX.contains(prefix)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isChinaUnicom(String str) {
		Matcher m = MOBILE_PATTERN.matcher(str);
		if (m.find()) {
			String prefix = m.group(1);
			if (MOBILE_10010.contains(prefix)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isChinaMobile(String str) {
		Matcher m = MOBILE_PATTERN.matcher(str);
		if (m.find()) {
			String prefix = m.group(1);
			if (MOBILE_10086.contains(prefix)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isChinaTelecom(String str) {
		Matcher m = MOBILE_PATTERN.matcher(str);
		if (m.find()) {
			String prefix = m.group(1);
			if (MOBILE_10000.contains(prefix)) {
				return true;
			}
		}
		return false;
	}

	public static boolean orHasText(String... strs) {
		if (strs == null || strs.length == 0) {
			return false;
		}
		for (String str : strs) {
			if (str != null && str.trim().length() > 0) {
				return true;
			}
		}
		return false;
	}

	public static boolean andHasText(String... strs) {
		if (strs == null || strs.length == 0) {
			return false;
		}

		for (String str : strs) {
			if (str == null || str.trim().length() == 0) {
				return false;
			}
		}
		return true;
	}

	public static boolean isEmpty(Collection<?> coll) {
		return coll == null || coll.size() == 0;
	}

	public static boolean isEmpty(Object[] array) {
		return array == null || array.length == 0;
	}

	public static boolean isEmpty(int[] array) {
		return array == null || array.length == 0;
	}

	public static boolean isEmpty(long[] array) {
		return array == null || array.length == 0;
	}

	public static boolean isEmpty(float[] array) {
		return array == null || array.length == 0;
	}

	public static boolean isEmpty(double[] array) {
		return array == null || array.length == 0;
	}

	public static boolean isEmpty(byte[] array) {
		return array == null || array.length == 0;
	}

	public static boolean isEmpty(char[] array) {
		return array == null || array.length == 0;
	}

	public static boolean isEmpty(boolean[] array) {
		return array == null || array.length == 0;
	}

	public static void main(String[] args) {
		System.out.println(Validate.isIp("1.1111.1.1"));
	}
}
